package com.ddwl.wallet.dao;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.WalletConstant;
import com.ddwl.schema.bo.wallet.WithdrawListQueryBo;
import com.ddwl.schema.util.DateUtil;
import com.ddwl.wallet.dao.mapper.UserWithdrawRecordMapper;
import com.ddwl.wallet.dao.model.UserWithdrawRecord;
import com.ddwl.common.dao.TkDao;
import com.ddwl.common.util.MySqlUtil;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.List;

/**
 * 提现渠道
 */
@Repository
public class WithdrawRecordDao extends TkDao<UserWithdrawRecordMapper, UserWithdrawRecord> {

    @Resource
    private UserWithdrawRecordMapper userWithdrawRecordMapper;

    public List<UserWithdrawRecord> queryWithdrawRecord(WithdrawListQueryBo bo){
        Example example = getExample();
        example.and()
                .andEqualTo("userId",bo.getUserId())
                .andEqualTo("channelType",bo.getChannelType())
                .andEqualTo("status",bo.getStatus())
                .andLike("userName", MySqlUtil.rightLike(bo.getUserName()))
                .andLike("payFlow", MySqlUtil.rightLike(bo.getPayFlow()))
                .andLike("account", MySqlUtil.rightLike(bo.getAccount()))
                .andLike("accountName", MySqlUtil.rightLike(bo.getAccountName()))
                .andGreaterThanOrEqualTo("gmtCreated", bo.getStartTime())
                .andLessThan("gmtCreated",bo.getEndTime());
        return baseMapper.selectByExample(example);
    }

    public Integer queryWithdrawRecordCount(WithdrawListQueryBo bo){
        return baseMapper.queryWithdrawRecordCount(bo);
    }

    /**
     * 自然月提现成功次数
     * @param userId
     * @return
     */
    public Integer countMonthByUserId(String userId) {
        WithdrawListQueryBo bo=new WithdrawListQueryBo();
        bo.setUserId(userId);
        bo.setStatus(BaseEnum.ApplyEnum.SUCCEED.getCode());
        bo.setStartTime(DateUtil.getFirstDayMonth(Instant.now()));
        bo.setEndTime(Instant.now());
        return userWithdrawRecordMapper.countInMonth(bo);
    }

    /**
     * 自然天提现成功次数
     * @param userId
     * @return
     */
    public Integer countDayByUserId(String userId) {
        WithdrawListQueryBo bo=new WithdrawListQueryBo();
        bo.setUserId(userId);
        bo.setStatus(BaseEnum.ApplyEnum.SUCCEED.getCode());
        bo.setStartTime(DateUtil.getTimesMorning());
        bo.setEndTime(Instant.now());
        return userWithdrawRecordMapper.countInMonth(bo);
    }

    /**
     * 查询当前正在处理中的提现申请
     * @param userId
     * @return
     */
    public UserWithdrawRecord queryProcessingRecord(String userId) {
        Example example = getExample();
        example.and().andEqualTo("userId",userId)
        .andLessThan("status",BaseEnum.ApplyEnum.SUCCEED.getCode());
        example.orderBy("gmtCreated").desc();
        return baseMapper.selectOneByExample(example);
    }
}
